package com.example.bbang.controller;

import com.example.bbang.common.CommonResult;
import com.example.bbang.entity.User;
import com.example.bbang.mapper.UserMapper;
//import com.example.bbang.redisTest.UserRedisTestService;
import com.example.bbang.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import util.CreateJwt;

import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
//文件
//@Value("${file-save-path}")
//private String fileSavePath;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");


    @Resource
    UserService userService;
    @Resource
    UserMapper userMapper;

//    @Resource
//    UserRedisTestService userRedisTestService;
//管理员
    @GetMapping
    public CommonResult getUser(){
      return CommonResult.ok(userService.findAll()) ;
    }
//其他
    @GetMapping("/qt")
    public CommonResult getElseUser(@RequestParam String username){
        System.out.println("拿自己username"+username);
    return CommonResult.ok(userService.findElseAll(username)) ;
//        return CommonResult.ok(new User());
}

//分页拿user
//    分页查询
//    接口路径：/user/page?pageNum=1&pageSize=10
//    @RequestParam接收
//    limit第一个参数= （pageNum-1)+pageSize
@GetMapping("/page")
public CommonResult findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
    System.out.println(pageNum+"和"+pageSize);
   // pageNum = (pageNum-1);
    return CommonResult.ok(userMapper.selectPage(pageNum,pageSize)) ;

}



    @PostMapping("/register")
    public CommonResult addUser(User user){
        User u= userService.save(user);
        if (u==null){
            return CommonResult.ok("添加失败");
        }

        return CommonResult.ok("添加成功") ;

    }





    @PostMapping ("/update")
    public CommonResult updateUser(User user){
        userService.updateById(user);
        return CommonResult.ok("success") ;
    }
//改密码
    @PostMapping ("/updatePassword")
    public CommonResult updatePassword(User user){
        User p=userService.updateByPassword(user);
        if(p==null){
            return CommonResult.ok("failed") ;
        }else {
        return CommonResult.ok("success") ;}
    }


    @PostMapping("/delete")
    public CommonResult deleteUser(Integer userID){
        System.out.println(userID);
        userService.deleteById(userID);
        return CommonResult.ok("success") ;
    }


    @GetMapping("/login")
    public @ResponseBody CommonResult login(@RequestParam String username, @RequestParam String password, @RequestParam Integer status, HttpServletRequest req){

        User user = userService.login(username,password,status);
        System.out.println(username+"+"+password+"+"+status);
        if (user == null){
            return CommonResult.ok("failed") ;
        }
       req.getServletContext().setAttribute("User",user);
        System.out.println("userSession11111"+req.getServletContext().getAttribute("User"));
        System.out.println("登陆成功");
        return CommonResult.ok(CreateJwt.gettoken(user)) ;
    }



    @GetMapping("/logout")
    public @ResponseBody CommonResult logout(HttpServletRequest req){
        req.getServletContext().removeAttribute("User");
        System.out.println("退出登陆");
        System.out.println("userSession4444"+req.getServletContext().getAttribute("User"));
        return CommonResult.ok("退出登陆") ;
    }


    //管理员查看所有帮手信息
    @GetMapping("/bsuser")
    public CommonResult getBsUser(){
        return CommonResult.ok(userService.findBsUser()) ;
    }


    //模糊搜索
    @GetMapping("/ms")
    @ResponseBody
    public CommonResult ms(String username){

//        if (o==null){
//            return CommonResult.ok("数据库中无此相关数据");
//        }
//        return CommonResult.ok("已查询成功");
        System.out.println(username);
        return CommonResult.ok(userService.ms(username));
    }


    @GetMapping("/pagename")
    public CommonResult findPageByUser(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String username){
        System.out.println(pageNum+"和"+pageSize+"和"+username);
        // pageNum = (pageNum-1);
        return CommonResult.ok(userMapper.findPageByUser(pageNum,pageSize,username)) ;

    }
    @GetMapping("/bspagename")
    public CommonResult findPageByBSUser(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String username){
        System.out.println(pageNum+"和"+pageSize+"和"+username);
        // pageNum = (pageNum-1);
        return CommonResult.ok(userMapper.findPageByBSUser(pageNum,pageSize,username)) ;

    }

    @GetMapping("/getUserCountByStatus")
    public CommonResult getUserCountByStatus(Integer status){
        return CommonResult.ok(userService.getUserCountByStatus(status));
    }

    @GetMapping("/getUserByUsername")
    public CommonResult getUserByUsername(@RequestParam String username){
        System.out.println(username);
        return CommonResult.ok(userService.getUserByUsername(username));
    }




}
